package pdfsetter.core.schema;

import java.util.ArrayList;
import java.util.List;

import pdfsetter.core.schema.onefile.OneFileDocumentSchemaPattern;

/**
 * This class represents automatic document pattern, i.e. pattern which can be applied to any number of files. We prepare set of schemas which we can apply to
 * single file - AutomaticDocumentSchemaPattern just applies it to every document (from first to last) and the resulting DocumentSchema will be composition of
 * each document processed with given one file schemas.
 * 
 * @author Andrzej Dorobisz
 */
public class AutomaticDocumentSchemaPattern implements DocumentSchemaPattern {
    OneFileDocumentSchemaPattern[] documentPatterns;

    public AutomaticDocumentSchemaPattern(List<OneFileDocumentSchemaPattern> oneFileDocumentPatterns) {
        this.documentPatterns = new OneFileDocumentSchemaPattern[oneFileDocumentPatterns.size()];
        int i = 0;
        for (OneFileDocumentSchemaPattern docPattern : oneFileDocumentPatterns) {
            documentPatterns[i++] = docPattern;
        }
    }

    @Override
    public DocumentSchema produceDocumentSchema(int[] fileSizes) {
        List<DocumentSchema> documentSchemas = new ArrayList<DocumentSchema>(fileSizes.length * documentPatterns.length);
        for (int fileNum = 0; fileNum < fileSizes.length; fileNum++) {
            for (OneFileDocumentSchemaPattern docPattern : documentPatterns) {
                documentSchemas.add(docPattern.produceDocumentSchema(fileNum, fileSizes[fileNum]));
            }
        }
        return new CompoundDocumentSchema(documentSchemas);
    }

    @Override
    public int getExpectedNumberOfFiles() {
        return -1;
    }

}
